package com.foreknow.dao.impl;

import com.foreknow.dao.CartDao;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;
import com.foreknow.model.Cart;

import java.util.List;

/**
 * SELECT * FROM cart c LEFT JOIN food f ON c.foodId = f.foodId
 * LEFT JOIN business b ON c.businessId = b.businessId
 * WHERE c.userId=8080 AND c.businessId = 10001
 */
public class CartDaoImpl extends BaseDao implements CartDao {
    @Override
    public List<Object> listCart(Cart cart) {
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT * ");
        sql.append("FROM cart c LEFT JOIN food f ON c.foodId = f.foodId ");
        sql.append("LEFT JOIN business b ON c.businessId = b.businessId ");
        sql.append("WHERE c.userId=?");
        if(cart.getBusinessId()!=null) {
            sql.append(" AND c.businessId =" + cart.getBusinessId());
        }
        EntityMapping mapping = mappingFactory.getMapping(MappingFactory.CART_MAPPING);
        List<Object> list = jt.query(sql.toString(),mapping,cart.getUserId());
        return list;
    }
}
